package cn.ibizlab.core.authentication.mapper;

import cn.ibizlab.core.ad.domain.SysPerson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ldap.core.AttributesMapper;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class LdapUserMapper implements AttributesMapper<SysPerson> {
 
    @Override
    public SysPerson mapFromAttributes(Attributes attributes) throws NamingException {
        SysPerson person = new SysPerson();
        for(Map.Entry<String,String> entry : LdapUserMapper.ldapFieldMaps.entrySet()){
            String ldapFiled = entry.getKey();
            String databaseFiled = entry.getValue();
            person.set(databaseFiled, attributes.get(ldapFiled) != null ? attributes.get(ldapFiled).get(): null);
        }
        return person;
    }

    /**
     * ldap属性映射表 key -> ldap field , value -> database field
     */
    public static final Map<String, String> ldapFieldMaps = new HashMap<String, String>(){{
        put("cn","uid");
        put("sn","displayName");
        put("ou","organizationId");
        put("o","organizationName");
        put("mail","mail");
        put("mobile","telephoneNumber");
        put("displayName","displayName");
        put("employeeNumber","employeeNumber");
        put("employeeType","employeeType");
        put("userPassword","userPassword");
    }};

}